package com.example.day4rxjava;

import android.os.Bundle;
import android.util.AndroidException;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.bt1)
    Button bt1;
    @BindView(R.id.bt2)
    Button bt2;
    @BindView(R.id.bt3)
    Button bt3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bt1, R.id.bt2, R.id.bt3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt1:  //标准观察者模式
                //被观察者
                Observable<String> o1=new Observable<String>() {
                    @Override
                    protected void subscribeActual(Observer<? super String> observer) {
                        //时尚编辑在看o1
                        observer.onNext("敲代码");
                        observer.onNext("唱歌");
                        observer.onNext("画动漫人物");
                        observer.onError(new Throwable("编辑被老板急急如律令召回公司了"));
                        observer.onComplete();
                        observer.onNext("练瑜伽");
                        observer.onComplete();

                    }
                };
                //观察者执行线程
                o1.observeOn(AndroidSchedulers.mainThread());
                //被观察者执行线程
                o1.subscribeOn(Schedulers.io());
                //观察者
                o1.subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String value) {
                        Toast.makeText(MainActivity.this, ""+value, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(MainActivity.this, ""+e, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });

                break;
            case R.id.bt2:  //快速创建操作符
                Observable<String> o2=new Observable<String>() {
                    @Override
                    protected void subscribeActual(Observer<? super String> observer) {
                        observer.onNext("敲代码");
                        observer.onNext("敲到半夜");
                        observer.onNext("晚安");
                    }
                };
                Observable<String> o3 = Observable.just("敲代码","敲到半夜","晚安");//快速创建了一个观察者
                //观察者
                o3.subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String value) {
                        Toast.makeText(MainActivity.this, ""+value, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(MainActivity.this, ""+e, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
                break;
            case R.id.bt3:  //合并操作符
                Observable<String> o4=new Observable<String>() {
                    @Override
                    protected void subscribeActual(Observer<? super String> observer) {
                        observer.onNext("敲代码");
                        observer.onNext("敲到半夜");
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        observer.onNext("晚安");
                        observer.onComplete();
                    }
                };
                Observable<String> o5=new Observable<String>() {
                    @Override
                    protected void subscribeActual(Observer<? super String> observer) {
                        observer.onNext("o5--敲代码");
                        observer.onNext("o5--敲到半夜");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        observer.onNext("o5--晚安");
                        observer.onComplete();
                    }
                };
                Observable.merge(o4,o5).observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .subscribe(new Observer<String>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(String s) {
                                Toast.makeText(MainActivity.this, ""+s, Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {
                                Toast.makeText(MainActivity.this, "Complete", Toast.LENGTH_SHORT).show();
                            }
                        });

                break;
        }
    }
}
